/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.util.log;

import java.io.IOException;
import java.io.OutputStream;

public class OutputStreamAppender extends Appender {

	private static OutputStreamAppender appender;

	/**
	 * 
	 * @param os
	 *            null if this appender has been initialised before
	 * @return
	 */
	public static OutputStreamAppender getAppender(OutputStream os) {
		if (appender == null) {
			if (os != null)
				appender = new OutputStreamAppender(os);
		}
		return appender;
	}

	private OutputStream os;

	private OutputStreamAppender(OutputStream os) {
		this.os = os;
	}

	protected void error(StringBuffer message) {
		write(message.append('\n'));
	}

	protected void fatal(StringBuffer message) {
		write(message.append('\n'));

	}

	protected void info(StringBuffer message) {
		write(message.append('\n'));
	}

	protected void warn(StringBuffer message) {
		write(message.append('\n'));
	}

	private void write(StringBuffer message) {
		try {
			os.write(message.toString().getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
